package de.java.shortys.utils.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

public final class FileUtils {

	public static File getFolderFromClassPath(ClassLoader loader, String filename, boolean createIfNotExists) {
		String filePath = null;
		File file = null;
		URL resource = loader.getResource(filename);
		if (resource != null) {
			filePath = resource.getFile();
			file = new File(filePath);
		} else if (createIfNotExists) {
			filePath = filename;
			file = new File(filePath);
			file.mkdir();
		}
		return file;
	}

	public static File getFileFromClassPath(ClassLoader loader, String filename) throws FileNotFoundException {
		String filePath = null;
		URL resource = loader.getResource(filename);
		if (resource != null) {
			filePath = resource.getFile();
			return new File(filePath);
		}

		throw new FileNotFoundException(filename);
	}

	private FileUtils() {
		// Empty
	}
}
